/**
 * Classname :Utilities.java
 * Version information: 1.0
 *
 */
package com.viviet.mobileservices.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.security.context.SecurityContextHolder;

import com.viviet.mobileservices.domain.model.WebUserDetails;

public abstract class Utilities {
	private static final Logger LOG = Logger.getLogger(Utilities.class);

	public static synchronized String generateTransactionID(String customerId, String lastTransactionId) {
		Long counter = Long.valueOf(lastTransactionId.substring(1));
		String countString = counter.toString();
		String transactionID = "" + "T";
		String []magicZeros = {
				"",					// 0
				"0",				// 1
				"00",				// 2	
				"000",				// 3
				"0000",				// 4
				"00000",			// 5
				"000000",			// 6
				"0000000",			// 7
				"00000000",			// 8
				"000000000"			// 9
		};		
		transactionID += magicZeros[9 - countString.length()];
		transactionID += countString;
		
		return transactionID;
	}
	
	/**
	 * get current vistar's user
	 * 
	 * @return
	 */
	public static WebUserDetails getWebUserDetails() {
		try {
			if (SecurityContextHolder.getContext() != null
					&& SecurityContextHolder.getContext().getAuthentication() != null) {
				Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
				if (obj != null && obj instanceof WebUserDetails) {
					return (WebUserDetails) obj;
				}
			}
		} catch (Exception ex) {
			LOG.debug("getWebUserDetails() - " + ex.getMessage(), ex);
		}
		return null;
	}
	
	public static boolean hasSepecialCharacters (String str) {
		Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		return m.find();
	}
	
	
	
}